package com.open.system.controller;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.open.system.domain.LoanProductDO;
import com.open.system.domain.LoanShopDO;
import com.open.system.service.LoanAdminLogService;
import com.open.system.service.LoanApplyLogService;
import com.open.system.service.LoanShopService;
import com.open.common.controller.BaseController;
import com.open.common.utils.PageUtils;
import com.open.common.utils.Query;
import com.open.common.utils.R;

/**
 */
 
@Controller
@RequestMapping("/system/loanShop")
public class LoanShopController extends BaseController {
	@Autowired
	private LoanShopService loanShopService;
	
	@Autowired
	private LoanApplyLogService loanApplyLogService;
	
	@Autowired
	private LoanAdminLogService loanAdminLogService;
	
	@GetMapping()
	@RequiresPermissions("system:loanShop:loanShop")
	String LoanShop(){
	    return "system/loanShop/loanShop";
	}
	
	
	@GetMapping("/LoanShopFenShop")
	@RequiresPermissions("system:loanShop:loanShop")
	String LoanShopFenShop(){
	    return "system/loanShop/LoanShopFenShop";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("system:loanShop:loanShop")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//--操作日志--开始--
		String shopId=getUser().getEmail();
		String adminName=getUser().getName();
		String Module="系统名称";
		String detail="查看了系统名称列表";
		loanAdminLogService.saveLog(shopId, adminName, Module, detail);
		//--操作日志--结束--
		 if(shopId!=null && shopId!="" && !"1".equals(shopId)){
				params.put("id",shopId);
		 }
		//查询列表数据
        Query query = new Query(params);
		List<LoanShopDO> loanShopList = loanShopService.list(query);
		int total = loanShopService.count(query);
		PageUtils pageUtils = new PageUtils(loanShopList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("system:loanShop:add")
	String add(){
	    return "system/loanShop/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("system:loanShop:edit")
	String edit(@PathVariable("id") Long id,Model model){
		LoanShopDO loanShop = loanShopService.get(id);
		model.addAttribute("loanShop", loanShop);
	    return "system/loanShop/edit";
	}
	
	
	@GetMapping("/editFenShop/{id}")
	@RequiresPermissions("system:loanShop:edit")
	String editFenShop(@PathVariable("id") Long id,Model model){
		LoanShopDO loanShop = loanShopService.get(id);
		model.addAttribute("loanShop", loanShop);
	    return "system/loanShop/editFenShop";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("system:loanShop:add")
	public R save( LoanShopDO loanShop){
		loanShop.setCreateTime(new Date());
		loanShop.setStatus("0");
		if(loanShopService.save(loanShop)>0){
			loanShop.setRemark1("http://www.ztamc001.com/#/index/"+loanShop.getId());
			//门店邀请码
			String requestStrCode=null;
			int idlength=loanShop.getId().toString().length();
			int needLength=4-idlength;
			StringBuffer num=new StringBuffer();
			StringBuffer numStr=new StringBuffer();
			for (int i = 0; i <needLength; i++) {
				num.append(9);
				numStr.append("0");
			}
			NumberFormat nf = new DecimalFormat(numStr.toString());
			requestStrCode=nf.format(new Random().nextInt(Integer.valueOf(num.toString()).intValue()));
			requestStrCode=requestStrCode+loanShop.getId().toString();
			loanShop.setRemark2(requestStrCode);
			loanShopService.update(loanShop);
			
			//--操作日志--开始--
			String shopId=getUser().getEmail();
			String adminName=getUser().getName();
			String Module="系统名称";
			String detail="新增了系统名称【"+loanShop.getShopName()+"】";
			loanAdminLogService.saveLog(shopId, adminName, Module, detail);
			//--操作日志--结束--
			
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("system:loanShop:edit")
	public R update( LoanShopDO loanShop){
		LoanShopDO loanShopParam=loanShopService.get(loanShop.getId());
		//门店邀请码
		if(StringUtils.isEmpty(loanShopParam.getRemark2())){
			String requestStrCode=null;
			int idlength=loanShop.getId().toString().length();
			int needLength=4-idlength;
			StringBuffer num=new StringBuffer();
			StringBuffer numStr=new StringBuffer();
			for (int i = 0; i <needLength; i++) {
				num.append(9);
				numStr.append("0");
			}
			NumberFormat nf = new DecimalFormat(numStr.toString());
			requestStrCode=nf.format(new Random().nextInt(Integer.valueOf(num.toString()).intValue()));
			requestStrCode=requestStrCode+loanShop.getId().toString();
			loanShop.setRemark2(requestStrCode);
			
			//--操作日志--开始--
			String shopId=getUser().getEmail();
			String adminName=getUser().getName();
			String Module="系统名称";
			String detail="更新了系统名称【"+loanShopParam.getShopName()+"】";
			loanAdminLogService.saveLog(shopId, adminName, Module, detail);
			//--操作日志--结束--
		}
		loanShopService.update(loanShop);
		return R.ok();
	}
	
	@GetMapping("/editCredit/{id}")
    @RequiresPermissions("system:loanShop:edit")
    String editcontract(@PathVariable("id") Long id, Model model) {
		LoanShopDO loanShopParam=loanShopService.get(id);
        model.addAttribute("loanShop", loanShopParam);
        return "system/loanShop/editCredit";
    }
	
	
	//委托担保申请
	@GetMapping("/editApplyCredit/{id}")
    @RequiresPermissions("system:loanShop:edit")
    String editApplyCredit(@PathVariable("id") Long id, Model model) {
		LoanShopDO loanShopParam=loanShopService.get(id);
        model.addAttribute("loanShop", loanShopParam);
        return "system/loanShop/editApplyCredit";
    }
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("system:loanShop:remove")
	public R remove( Long id){
		if(loanShopService.remove(id)>0){
			//--操作日志--开始--
			String shopId=getUser().getEmail();
			String adminName=getUser().getName();
			String Module="系统名称";
			String detail="删除了系统名称【"+id+"】";
			loanAdminLogService.saveLog(shopId, adminName, Module, detail);
			//--操作日志--结束--
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 修改是否显示邀请码
	 */
	@PostMapping( "/codeShow")
	@ResponseBody
	@RequiresPermissions("system:loanShop:remove")
	public R codeShow( Long id){
		
		LoanShopDO loanShopParam=loanShopService.get(id);
		if(loanShopParam.getRemark3()==null || loanShopParam.getRemark3()==""||"0".equals(loanShopParam.getRemark3())){
			loanShopParam.setRemark3("1");
		}else if("1".equals(loanShopParam.getRemark3())){
			loanShopParam.setRemark3("0");
		}
		loanShopService.update(loanShopParam);
		
		//--操作日志--开始--
		String shopId=getUser().getEmail();
		String adminName=getUser().getName();
		String Module="系统名称";
		String detail="修改了【"+loanShopParam.getShopName()+"】是否显示邀请码【"+id+"】";
		loanAdminLogService.saveLog(shopId, adminName, Module, detail);
		//--操作日志--结束--
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("system:loanShop:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] ids){
		loanShopService.batchRemove(ids);
		return R.ok();
	}
	
	
	public static String getRandString(int length){
        String charList = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
        String rev = "";
        Random f = new Random();
        for(int i=0;i<length;i++){
           rev += charList.charAt(Math.abs(f.nextInt())%charList.length());
        }
        return rev;
    }
	
}
